package com.woan.api.services;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;

import com.woan.api.util.DateJsonValueProcessor;
import com.woan.api.vo.VCustomvideoList;
import com.woan.api.vo.VadmininfoList;
import com.woan.api.vo.VmessageList;
import com.woan.api.vo.VpositionList;
import com.woan.api.vo.VvideoList;
import com.woan.api.idao.SysAdmininfoIDAO;
import com.woan.api.idao.SysMessageIDAO;
import com.woan.api.idao.ZbConfigIDAO;
import com.woan.api.idao.ZbControlIDAO;
import com.woan.api.model.SysAdmininfo;
import com.woan.api.model.SysMessage;
import com.woan.api.model.ZbConfig;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;



public class ZbconfigServerServices {
	private SysAdmininfoIDAO sysadmininfoiDAO;
	private ZbConfigIDAO zbconfigiDAO;
	public SysAdmininfoIDAO getSysadmininfoiDAO() {
		return sysadmininfoiDAO;
	}
	public void setSysadmininfoiDAO(SysAdmininfoIDAO sysadmininfoiDAO) {
		this.sysadmininfoiDAO = sysadmininfoiDAO;
	}

	public ZbConfigIDAO getZbconfigiDAO() {
		return zbconfigiDAO;
	}
	public void setZbconfigiDAO(ZbConfigIDAO zbconfigiDAO) {
		this.zbconfigiDAO = zbconfigiDAO;
	}
	public JSONObject saveVideoConfig(String rid,String config){
		boolean flag = false;
		if(!config.equals(""))
		{
			//先清除历史配置
			zbconfigiDAO.DeleteVideoConfig(rid);
			ZbConfig zbconfig= new ZbConfig();
			//config为一串配置的字符串，中间用","分隔
			
			String []configarr = config.split(",");
			for(int i=0;i<configarr.length;i++)
			{
				//将config拆分成数组后，每个元素代表一个配置，中间用"@"分隔，顺序为"postion@username@message"
				
				String []temp = configarr[i].split("@");
				zbconfig.setDevice("ipad");
				zbconfig.setGroupId(rid);
				zbconfig.setMessage(temp[2]);
				zbconfig.setUserName(temp[1]);
				zbconfig.setPosition(temp[0]);
				flag = zbconfigiDAO.SaveVideoConfig(zbconfig);
			}
		}
		
		JSONObject json=new JSONObject();
		if(flag)
		{
			json.accumulate("results", 0);
			json.accumulate("errMsg", "配置成功");
		}
		else
		{
			json.accumulate("results", 1);
			json.accumulate("errMsg", "配置失败");
		}
		return json;
	}
	public List<ZbConfig> listConfig(String rid){
		List<ZbConfig> zbconfig = null;
		zbconfig = zbconfigiDAO.FindListConfig(rid);
		return zbconfig;
	}
	
	public List<VCustomvideoList> listConfigLiveVideos(String rid){
		List<VCustomvideoList> video = null;
		video = zbconfigiDAO.FindConfigLiveVideos(rid);
		return video;
	}
}
